rm(list=ls())
library(foreign)
library(ggplot2) 
library(gridExtra)
library(knitr)
library(grid)
library(dplyr)
library(forcats)
library(haven)
library(grid)
library(gridExtra)
library(ggtext)
library(viridis)



theme_results <- theme_bw(base_size = 19) +
  theme(text=element_text(family="Times",
                          size = 19),
        axis.text.x = element_text(size = 19, color = "black"),
        axis.text.y = element_text(size = 15, color = "black",angle=0),
        axis.title.y = element_text(size = 19, color = "black"),
        axis.title.x = element_text(size = 17, color = "black"),
        legend.position = "bottom",
        legend.key.size = unit(1.2, "cm"),
        legend.text = element_text(size = 17),
        legend.title = element_text(size = 1),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        strip.background = element_rect(fill = "white"))



palette2 <- c("black", "gray", "blue", "#0098DB")
palette3<- c("black","gray","blue")
palette_twolines <- c("black", "gray")
palette6<- c("#1B5E20", "#388E3C", "#66BB6A", "#6A1B9A", "#8E24AA", "#E1BEE7")
palette_facet <- c("black", "dodgerblue2", "grey40")

setwd("/Users/Ana/Desktop/")


# ----------------------------------------------------------------------------------------------------------------#
#                                                                                                                 #
#                                              FIGURES IN MANUSCRIPT                                              #
#                                                                                                                 #
# ----------------------------------------------------------------------------------------------------------------#

main <- read_dta("main_hte.dta")

main$all <- factor(main$var2,
                       levels = c(1,2,3,4,5,6,7,8,9, 10,11,12,13,14,15,16,17,18,19,20,21,22,23),
                       labels = c( "Residents care abt. big parking",
                                   "Residents care abt. parks",
                                   "Residents care abt. fighting crime",
                                   "Residents care abt. racial equality",
                                   "(Respondent support BLM)",
                                   "Residents support BLM",
                                   "Residents support secession",
                                   "Residents proud Southerners",
                                   "Residents vote Republican",
                                   "Town has conflictive history",
                                   "Joy",
                                   "Pride",
                                   "Sadness",
                                   "Hostility",
                                   "Fear",
                                   "Disgust",
                                   "Anger",
                                   "Quantity donated to schools",
                                   "Donation yes/no",
                                   "Good place to live",
                                   "Self-value in community",
                                   "External efficacy in the town",
                                   "Trust people in the town"))


main$typeout <- factor(main$typeout,
                      levels = c(1,2,3),
                      labels = c( "Signals",
                                  "Emotions",
                                  "Attitudes & Quasi-behavioral"))


main$dataset <- factor(main$dataset,
                      levels = c(0,1,2),
                      labels = c( "Main effect",
                                  "Effect by race",
                                  "Effect by Party ID"))

main$subset <- factor(main$subset,
                  levels = c(0,1,2),
                  labels = c("Full sample",
                             "Black/Democrat",
                             "White/Republican"))

main$analysis <- factor(main$analysis,
                     levels = c(0,1,2),
                     labels = c("Main effect",
                                "Estimate per group",
                                "Difference in the effect of Treatment"))

palette_facet <- c("black", "red", "grey40")


# ----------------------------------------------------------------------------------------------------------------#
# FIGURE 5. SIGNALS: Main effect, race and party ID                                                               #
# ----------------------------------------------------------------------------------------------------------------#

main_signals <- main[which(main$typeout == "Signals" & main$Controls == 0 & main$analysis != "Difference in the effect of Treatment"),]

plot_main_signals <- main_signals %>%
  ggplot(aes(x = as.factor(all), y = coef,
             color = subset, shape = subset)) +   
  geom_point(size = 2.5 ,position = position_dodge(0.35)) +
  geom_errorbar(aes(ymax = ci_upper, ymin = ci_lower), width = 0, size = .5, position = position_dodge(width = 0.35)) +
  ylab("Effect of exposure to Confederate commemorations (Standardized)") +  xlab("") +
  scale_color_manual(name = "Legend", values = c("black", "black", "gray60")) +
  scale_shape_manual(name = "Legend", values = c(16, 17, 18)) +  # Update shape values to be distinct
  geom_hline(yintercept = 0, linetype = 2, color = "red", alpha = .4) + 
  facet_wrap(~dataset) +
  coord_flip() +
  theme_results +
  theme(strip.text = element_text(size = 12),
        axis.text.x = element_text(size = 12, color = "black"),
        axis.text.y = element_text(size = 12, color = "black", angle = 0),
        legend.text = element_text(size = 10)) +
  guides(color = guide_legend(override.aes = list(shape = c(16, 17, 18))))

plot_main_signals

ggsave("/Users/Ana/Desktop/plot_main_signals.png", plot = plot_main_signals , width = 35, height = 17, units = "cm")


# ----------------------------------------------------------------------------------------------------------------#
# FIGURE 6. EMOTIONS AND ATTITUDES & SEMI-BEHAVIORAL: Main effect, race and party ID                              #
# ----------------------------------------------------------------------------------------------------------------#

main_emoatti <- main[which(main$typeout != "Signals"& main$Controls == 0 & main$analysis != "Difference in the effect of Treatment"),]

plot_main_emoatti <- main_emoatti %>%
  ggplot(aes(x = as.factor(all), y = coef,
             color = subset, shape = subset)) +   
  geom_point(size = 2.5 ,position = position_dodge(0.35)) +
  geom_errorbar(aes(ymax = ci_upper, ymin = ci_lower), width = 0, size = .5, position = position_dodge(width = 0.35)) +
  ylab("Effect of exposure to Confederate commemorations (Standardized)") +  xlab("") +
  scale_color_manual(name = "Legend", values = c("black", "black", "gray60")) +
  scale_shape_manual(name = "Legend", values = c(16, 17, 18)) +
  geom_hline(yintercept = 0, linetype = 2, color = "red", alpha = .4) + 
  facet_grid (typeout ~ dataset, scales = "free_y") +  # Use facet_grid instead of facet_wrap for multiple facets
  coord_flip() +
  theme_results +
  theme(strip.text = element_text(size = 12),
        axis.text.x = element_text(size = 12, color = "black"),
        axis.text.y = element_text(size = 12, color = "black", angle = 0),
        legend.text = element_text(size = 10)) +
  guides(color = guide_legend(override.aes = list(shape = c(16, 17, 18))))

plot_main_emoatti

ggsave("/Users/Ana/Desktop/plot_main_emoatti.png", plot = plot_main_emoatti , width = 35, height = 25, units = "cm")





# ----------------------------------------------------------------------------------------------------------------#
#                                                                                                                 #
#                                              FIGURES IN APPENDIX                                                #
#                                                                                                                 #
# ----------------------------------------------------------------------------------------------------------------#


# ----------------------------------------------------------------------------------------------------------------#
# FIGURE A1. Heterogenous Treatment Effects by Race                                                               #
# ----------------------------------------------------------------------------------------------------------------#

race <- read_dta("race.dta")

race$all <- factor(race$var2,
                        levels = c(1,2,3,4,5,6,7,8,9, 10,11,12,13,14,15,16,17,18,19,20,21,22,23),
                        labels = c( "Residents care abt. big parking",
                                    "Residents care abt. parks",
                                    "Residents care abt. fighting crime",
                                    "Residents care abt. racial equality",
                                    "(Respondent support BLM)",
                                    "Residents support BLM",
                                    "Residents support secession",
                                    "Residents proud Southerners",
                                    "Residents vote Republican",
                                    "Town has conflictive history",
                                    "Joy",
                                    "Pride",
                                    "Sadness",
                                    "Hostility",
                                    "Fear",
                                    "Disgust",
                                    "Anger",
                                    "Quantity donated to schools",
                                    "Donation yes/no",
                                    "Good place to live",
                                    "Self-value in community",
                                    "External efficacy in the town",
                                    "Trust people in the town"))



race$controls <- factor(race$Controls,
                             levels = c(0,2),
                             labels = c("No controls",
                                        "Atenttion and town-recognition checks"))

race$subset <- factor(race$subset,
                         levels = c(1,2,0),
                         labels = c("Black",
                                    "White",
                                    "Black vs White"))

race$analysis <- factor(race$analysis,
                          levels = c(1,2),
                          labels = c("Estimate",
                                     "Difference in the effect of Treatment"))


race$typeout <- factor(race$typeout,
                           levels = c(1,2,3),
                           labels = c("Signals",
                                      "Emotions",
                                      "Attitudes &\n Quasi-behavioral"))


race <- race[which(race$Controls== "0"),]

plot_race <- race %>%
  ggplot(aes(x = as.factor(all), y = coef,
             color = subset, shape = subset)) +   
  geom_point(size = 2.5, position = position_dodge(0.35)) +
  geom_errorbar(aes(ymax = ci_upper, ymin = ci_lower), width = 0, size = .5, position = position_dodge(width = 0.35)) +
  ylab("Estimates per group and difference in the effect of the treatment (Standardized)") +  xlab("") +
  scale_color_manual(name = "", values = c("black",  "gray60","black" )) +
  scale_shape_manual(name = "", values = c(16, 17, 18)) +  # Use distinct shape values
  geom_hline(yintercept = 0, linetype = 2, color = "red", alpha = 0.4) + 
  facet_grid(~typeout~analysis, scales = "free_y") +
  coord_flip() +
  theme_results +
  theme(strip.text = element_text(size = 12),
        axis.text.x = element_text(size = 12, color = "black"),
        axis.text.y = element_text(size = 12, color = "black", angle = 0),
        legend.text = element_text(size = 10))

plot_race



ggsave("/Users/Ana/Desktop/plot_race.png", plot = plot_race , width = 30, height = 35, units = "cm")

# ----------------------------------------------------------------------------------------------------------------#
# FIGURE A2. Heterogenous Treatment Effects by Party ID                                                           #
# ----------------------------------------------------------------------------------------------------------------#

party <- read_dta("party.dta")

party$all <- factor(party$var2,
                            levels = c(1,2,3,4,5,6,7,8,9, 10,11,12,13,14,15,16,17,18,19,20,21,22,23),
                            labels = c( "Residents care abt. big parking",
                                        "Residents care abt. parks",
                                        "Residents care abt. fighting crime",
                                        "Residents care abt. racial equality",
                                        "(Respondent support BLM)",
                                        "Residents support BLM",
                                        "Residents support secession",
                                        "Residents proud Southerners",
                                        "Residents vote Republican",
                                        "Town has conflictive history",
                                        "Joy",
                                        "Pride",
                                        "Sadness",
                                        "Hostility",
                                        "Fear",
                                        "Disgust",
                                        "Anger",
                                        "Quantity donated to schools",
                                        "Donation yes/no",
                                        "Good place to live",
                                        "Self-value in community",
                                        "External efficacy in the town",
                                        "Trust people in the town"))

party$controls <- factor(party$Controls,
                                 levels = c(0,2),
                                 labels = c("No controls",
                                            "Atenttion and town-recognition checks"))

party$subset <- factor(party$subset,
                             levels = c(1,2,0),
                             labels = c("Democrat",
                                        "Republican",
                                        "Democrat vs Republican"))

party$analysis <- factor(party$Facet,
                              levels = c(1,2),
                              labels = c("Estimate",
                                         "Difference in the effect of Treatment"))

party$typeout <- factor(party$typeout,
                               levels = c(1,2,3),
                               labels = c("Signals",
                                          "Emotions",
                                          "Attitudes &\n Quasi-behavioral"))

party <- party[which(party$Controls== "0"),]
plot_party <- party %>%
  ggplot(aes(x = as.factor(all), y = coef,
             color = subset, shape = subset)) +   
  geom_point(size = 2.5 ,position = position_dodge(0.35)) +
  geom_errorbar(aes(ymax = ci_upper, ymin = ci_lower), width = 0, size = .5, position = position_dodge(width = 0.35)) +
  ylab("Estimates per group and difference in the effect of the treatment (Standardized)") +  xlab("") +
  scale_color_manual(name = "", values = c("black",  "gray60","black" )) +
  scale_shape_manual(name = "", values = c(16, 17, 18)) +  # Use distinct shape values
  geom_hline(yintercept = 0, linetype = 2, color = "red", alpha = 0.4) + 
  facet_grid(~typeout~analysis, scales="free_y") +
  coord_flip() +
  theme_results +
  theme(strip.text = element_text(size=12),
        axis.text.x = element_text(size = 12, color = "black"),
        axis.text.y = element_text(size = 12, color = "black",angle=0),
        legend.text = element_text(size = 10))
plot_party

ggsave("/Users/Ana/Desktop/plot_party.png", plot = plot_party , width = 30, height = 35, units = "cm")





# ----------------------------------------------------------------------------------------------------------------#
# FIGURE E1. Replication of analysis with respondents that declare to belong to a Deep South State                 #
# ----------------------------------------------------------------------------------------------------------------#


deepsouth <- read_dta("deepsouth.dta")

deepsouth$all <- factor(deepsouth$var2,
                            levels = c(1,2,3,4,5,6,7,8,9, 10,11,12,13,14,15,16,17,18,19,20,21,22,23),
                            labels = c( "Believes town cares abt. big parking",
                                        "Believes town cares abt. parks",
                                        "Believes town cares abt. fighting crime",
                                        "Believes town cares abt. racial equality",
                                        "Respondent supports BLM",
                                        "Believes town supports BLM",
                                        "Believes town supports secession",
                                        "Believes town proud of South",
                                        "Believes town votes Republican",
                                        "Believes town has conflictive history",
                                        "Joy",
                                        "Pride",
                                        "Sadness",
                                        "Hostility",
                                        "Fear",
                                        "Disgust",
                                        "Anger",
                                        "Quantity donated to schools",
                                        "Donation yes/no",
                                        "Good place to live",
                                        "Self-value in community",
                                        "External efficacy in the town",
                                        "Trust people in the town"))

deepsouth$controls <- factor(deepsouth$Controls,
                                 levels = c(0,2),
                                 labels = c("No controls",
                                            "Atenttion and town-recognition checks"))

deepsouth$subset <- factor(deepsouth$subset,
                             levels = c(1,2,0),
                             labels = c("Sense of belonging to a State\nin Deep South (Belong SDS)",
                                        "Not Belong SDS",
                                        "Belong SDS vs Not Belong SDS"))


deepsouth$analysis <- factor(deepsouth$analysis,
                              levels = c(1,2),
                              labels = c("Estimate",
                                         "Difference in the effect of Treatment"))

deepsouth$typeout <- factor(deepsouth$typeout,
                               levels = c(1,2,3),
                               labels = c("Signals",
                                          "Emotions",
                                          "Attitudes &\n Quasi-behavioral"))

deepsouth <- deepsouth[which(deepsouth$Controls== "0"),]

plot_deepsouth <- deepsouth %>%
  ggplot(aes(x = as.factor(all), y = coef,
             color = subset, shape = subset)) +   
  geom_point(size = 2.5 ,position = position_dodge(0.35)) +
  geom_errorbar(aes(ymax = ci_upper, ymin = ci_lower), width = 0, size = .9, position = position_dodge(width = 0.35)) +
  ylab("Estimates per group and difference in the effect of the treatment (Standardized)") +  xlab("") +
  scale_color_manual(name = "", values = c("black",  "gray60","black" )) +
  scale_shape_manual(name = "", values = c(16, 17, 18)) +  # Use distinct shape values
  geom_hline(yintercept = 0, linetype = 2, color = "red", alpha = 0.4) + 
  facet_grid(~typeout~analysis, scales="free_y") +
  coord_flip() +
  theme_results +
  theme(strip.text = element_text(size=12),
        axis.text.x = element_text(size = 12, color = "black"),
        axis.text.y = element_text(size = 12, color = "black",angle=0),
        legend.text = element_text(size = 10))
plot_deepsouth

ggsave("/Users/Ana/Desktop/plot_deepsouth.png", plot = plot_deepsouth , width = 30, height = 35, units = "cm")

